﻿


Namespace YahooManaged.Finance.Indicators


    Public Class MACD
        Inherits EMA

        Public Overrides ReadOnly Property Name() As String
            Get
                Return "Moving Average Convergence/Divergence"
            End Get
        End Property

        Public Overrides ReadOnly Property IsRealative As Boolean
            Get
                Return True
            End Get
        End Property

        Public Property PeriodFast() As Integer = 12
        Public Property PeriodSlow() As Integer = 26


        Public Sub New()
            MyBase.Period = 9
        End Sub


        Public Overrides Function Calculate(ByVal values As System.Collections.Generic.IEnumerable(Of System.Collections.Generic.KeyValuePair(Of Date, Double))) As System.Collections.Generic.Dictionary(Of Date, Double)()
            Dim basePeriod As Integer = MyBase.Period
            Dim macdValues As New Dictionary(Of Date, Double)
            'Dim signalLineValues As New Dictionary(Of Date, Double)

            MyBase.Period = Me.PeriodFast
            Dim ema12values = MyBase.Calculate(values)(0)

            MyBase.Period = Me.PeriodSlow
            Dim ema26values = MyBase.Calculate(values)(0)

            Dim closeValues As New List(Of KeyValuePair(Of Date, Double))(values)

            Dim d As Date
            For i As Integer = 0 To closeValues.Count - 1
                d = closeValues(i).Key
                macdValues.Add(d, ema12values(d) - ema26values(d))
            Next

            MyBase.Period = basePeriod
            Dim ema9values = MyBase.Calculate(macdValues)(0)

            Return New Dictionary(Of Date, Double)() {macdValues, ema9values}
        End Function

        Public Overrides Function ToString() As String
            Return Me.Name & " " & Me.Period
        End Function

    End Class

End Namespace